﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;


namespace xDataSet

{
    public static class AESEncryption
    {
        //默认密钥向量 
        private static byte[] _key = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        //默认密钥
        public static string strKey = "BC6EFCB192CD9090F5D8AB2C6B2FBE6B";

        /// <summary>
        /// AES加密算法
        /// </summary>
        /// <param name="plainText">明文字符串</param>
        /// <returns>返回加密后的密文字节数组</returns>
        public static string AESEncrypt(string plainText)
        {
            //分组加密算法
            SymmetricAlgorithm des = Rijndael.Create();
            byte[] inputByteArray = Encoding.UTF8.GetBytes(plainText);//得到需要加密的字节数组	
            //设置密钥及密钥向量
            des.Key = Encoding.UTF8.GetBytes(strKey);
            des.IV = _key;
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            byte[] cipherBytes = ms.ToArray();//得到加密后的字节数组
            cs.Close();
            ms.Close();
            string cipher = Convert.ToBase64String(cipherBytes);
            return cipher;
        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="encryptText">密文</param>
        /// <returns>返回解密后的字符串</returns>
        public static string AESDecrypt(string encryptText)
        {
            byte[] cipherText = Convert.FromBase64String(encryptText);
            SymmetricAlgorithm des = Rijndael.Create();
            des.Key = Encoding.UTF8.GetBytes(strKey);
            des.IV = _key;
            byte[] decryptBytes = new byte[cipherText.Length];
            MemoryStream ms = new MemoryStream(cipherText);
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read);
            cs.Read(decryptBytes, 0, decryptBytes.Length);
            cs.Close();
            ms.Close();
            string decrypt = Encoding.UTF8.GetString(decryptBytes);
            return decrypt;
        }

        /// <summary>
        /// 信令牌加密
        /// </summary>
        /// <param name="plainText">明文字符串</param>
        /// <param name="secretKey">密钥</param>
        /// <returns>加密后的字符串</returns>
        public static string SignalingEncrypt(string plainText, string secretKey)
        {
            byte[] key = Encoding.Default.GetBytes(secretKey);
            byte[] iv = _key;
            byte[] plaintextBuffer = Encoding.UTF8.GetBytes(plainText);
            Rijndael rijndael = Rijndael.Create();
            ICryptoTransform transform = rijndael.CreateEncryptor(key, iv);
            byte[] cipherTextBuffer = transform.TransformFinalBlock(plaintextBuffer, 0, plaintextBuffer.Length);
            transform.Dispose();
            string strEn = Convert.ToBase64String(cipherTextBuffer);
            return strEn;
        }

        /// <summary>
        /// 信令牌解密
        /// </summary>
        /// <param name="encryptText">密文</param>
        /// <param name="secretKey">密钥</param>
        /// <returns>解密后的字符串</returns>
        public static string SignalingDecrypt(string encryptText, string secretKey)
        {
            byte[] key = Encoding.UTF8.GetBytes(secretKey);
            byte[] iv = _key;
            byte[] cipherTextBuffer = Convert.FromBase64String(encryptText);
            Rijndael rijndael = Rijndael.Create();
            ICryptoTransform transform = rijndael.CreateDecryptor(key, iv);
            byte[] decryption = transform.TransformFinalBlock(cipherTextBuffer, 0, cipherTextBuffer.Length);
            transform.Dispose();
            string strDe = Encoding.UTF8.GetString(decryption);
            return strDe.Trim('\0', '\t', '\n');
        }
    }
}

